package com.stox.client.core.application;

import com.stox.shared.model.exception.StoxException;



public abstract class AbstractApplication implements Application {

	private Status status = Status.DORMANT;
	
	@Override
	public void register() {
		if(!Status.DORMANT.equals(status)) throw new StoxException("Applicatican \""+getName()+"\" is already registered");
		status = Status.REGISTERED;
	}

	@Override
	public void start() {
		if(!Status.REGISTERED.equals(status)) throw new StoxException("Applicatican \""+getName()+"\" is in state : "+status+"\nCan not start");
		status = Status.STARTED;
	}

	@Override
	public void show() {
		if(!Status.STARTED.equals(status) && !Status.SHOWING.equals(status))
			throw new StoxException("Applicatican \""+getName()+"\" is in state : "+status+"\nCan not show ui components");
		status = Status.SHOWING;
	}

	@Override
	public void hide() {
		if(Status.SHOWING.equals(status)){
			status = Status.STARTED;
		}
	}

	@Override
	public void stop() {
		if(Status.SHOWING.equals(status) || Status.STARTED.equals(status)){
			hide();
			status = Status.REGISTERED;
		}
	}

	@Override
	public void unregister() {
		stop();
		status = Status.DORMANT;
	}
	
	@Override
	public Status getStatus() {
		return status;
	}

}
